from threading import Lock
import datetime
import time
class ErrorBuffer:
    def __init__(self, role):
        self.buf = []
        self.lock = Lock()
        self.role = role

    def clear(self):
        self.lock.acquire()
        self.buf.clear()
        self.lock.release()

    def store_error(self, key, value):
        self.lock.acquire()
        self.buf.append(key + ':' +value)
        self.lock.release()

    def constains_error(self):
        if len(self.buf) > 0:
            try:
                assert False, '\n\n%s:\n\n收到错误回调,请检查接口是否正确：\n%s' % (self.role, self.to_string())
            finally:
                self.clear()

    def to_string(self):
        self.lock.acquire()
        string = ''
        for ret in self.buf:
            string = string + ret + '\n'
        self.lock.release()
        return string



if __name__ == '__main__':
    errorbuf = ErrorBuffer()
    print(errorbuf.to_string())
